var utils = require("../utils/utils");
var ocr_utils = require("../utils/ocr_utils");
var create_ad = require("../utils/create_ad");
var point_map = require("./pointMap");

var bless = {
  go_page_from_home() {
    var logger = utils.create_logger("bless go_page_from_home: ");
    if (ocr_utils.screen_exists_text_all(["视频", "小说"], true)) {
      logger("进入我的");
      utils.click_point.apply(
        utils,
        utils.get_size_point(point_map["首页我的"])
      );
      sleep(utils.get_random_interval(3, 0, 2, 1000));
      if (utils.click_text_starts_with("邀请好友")) {
        logger("找到：邀请好友");
        sleep(utils.get_random_interval(5, 0, 2, 1000));
        this.init();
      } else {
        logger("没找到：邀请好友");
      }
    } else {
      logger("NOT FOUND");
    }
  },
  share_5_is_over: false,
  share_5() {
    var logger = utils.create_logger("task_bless share_5: ");
    var text_ui = utils.exists_text_ends_with("必得199金币");
    if (!text_ui) {
      logger("今天的-必得199金币-已经全部执行完毕");
      this.share_5_is_over = true;
      return;
    }
    for (var i = 0; i < 5; i++) {
      var text_ui = utils.exists_text_ends_with("必得199金币");
      if (text_ui) {
        logger("找到-必得199金币-");
        utils.click_text_ends_with("必得199金币");
        sleep(utils.get_random_interval(2, 0, 2, 1000));
        utils.click_text_starts_with("去粘贴");
        sleep(utils.get_random_interval(5, 0, 2, 1000));
        back();
        sleep(utils.get_random_interval(5, 0, 2, 1000));
        if (utils.exists_text("继续分享")) {
          logger("找到-继续分享-");
          utils.click_text("继续分享");
          sleep(utils.get_random_interval(2, 0, 2, 1000));
        } else if (utils.exists_text("开心收下")) {
          logger("找到-开心收下-");
          utils.click_text("开心收下");
          sleep(utils.get_random_interval(2, 0, 2, 1000));
        }
      }
    }
  },
  init() {
    var logger = utils.create_logger("bless start: ");
    var ocr_text = ocr_utils.screen_exists_text_one("开心收下");
    if (ocr_text) {
      logger("找到 ocr_text -开心收下-");
      utils.click_bounds(ocr_text.bounds);
      sleep(utils.get_random_interval(2, 0, 2, 1000));
    } else {
      logger("没找到ocr_text -开心收下-");
    }
    if (!this.share_5_is_over) {
      this.share_5(); //分享5次必得199金币
    }
    var ui_text = utils.click_text("开福袋");
    if (ui_text) {
      logger("找到【开福袋】");
      sleep(2 * 1000);
      this.run();
    } else {
      logger("没找到【开福袋】");
      this.after();
    }
  },
  run() {
    var logger = utils.create_logger("bless run: ");
    var start_time = Date.now();
    if (!utils.click_text_starts_with("看视频再领")) {
      logger("没找到-看视频再领-");
      return;
    }
    logger("找到-看视频再领-");
    sleep(random(1, 2) * 1000);
    var ad = create_ad({
      ad_min_interval: 40, //最小的看广告时间
      ad_max_go_back_times: 4, //广告结束之后最多的按返回键次数
      if_in_ad: null, //是否在广告页面
      if_in_start: function () {
        if (utils.exists_text_starts_with("立即收下")) {
          return true;
        }
        if (utils.exists_text_starts_with("开心收下")) {
          return true;
        }
        if (utils.exists_text_starts_with("面对面")) {
          return true;
        }
        return false;
      }, //是否在开始广告的页面
      if_in_home: function () {
        return ocr_utils.screen_exists_text_all(["视频", "小说"], true);
      }, //是否在应用首页
      if_has_next_ad: function () {
        var contents = [
          "再看一个领取",
          "再看一条领取",
          "再看1个获得",
          "换个视频",
        ];
        for (var index = 0; index < contents.length; index++) {
          var content = contents[index];
          if (utils.click_text_starts_with(content)) {
            logger("找到-" + content + "-");
            sleep(random(2, 3) * 1000);
            return true;
          }
        }
      }, //是否有下一个广告
      if_ad_is_over: function () {
        var contents = ["已领取", "已完成任务"];
        for (var index = 0; index < contents.length; index++) {
          var content = contents[index];
          if (utils.exists_text_starts_with(content)) {
            logger("找到-" + content + "-");
            return true;
          }
        }
      }, //是否广告已经结束
      exit_ad: function () {
        var contents = ["残忍离开"];
        for (var index = 0; index < contents.length; index++) {
          var content = contents[index];
          if (utils.click_text_starts_with(content)) {
            logger("找到-" + content + "-");
            return true;
          }
        }
      }, //退出广告页面
      max_ad_times: 10, //本轮最多的看广告次数
      log: true, //是否打印
    });
    ad.init();
    var contents = ["立即收下", "开心收下"];
    for (var index = 0; index < contents.length; index++) {
      var content = contents[index];
      if (utils.click_text_starts_with(content)) {
        logger("找到-" + content + "-");
        sleep(random(1, 2) * 1000);
        break;
      }
    }
    var end_time = Date.now();
    logger("耗时-" + (end_time - start_time) / 1000 + "-s");
    this.after();
  },
  after() {
    var logger = utils.create_logger("bless after: ");
    logger();
    sleep(utils.get_random_interval(2, 0, 2, 1000));
    back();
    sleep(utils.get_random_interval(5, 0, 2, 1000));
    var ocr_text = ocr_utils.screen_exists_text_one("放弃奖励");
    if (ocr_text) {
      back();
      sleep(utils.get_random_interval(2, 0, 2, 1000));
    }
    this.next();
  },
  next() {
    var logger = utils.create_logger("bless next: ");
    logger();
  },
};
module.exports = bless;
